/**
 *====================================================
 * 文件名称: InsuranceServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.project.entity.InsuranceEntity;
import com.yuexunit.finance.project.entity.lexicon.InsuranceStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;

/**
 * @ClassName: InsuranceServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(InsuranceService.class)
public class InsuranceServiceImpl implements InsuranceService {

	@Override
	public Long createFinanceInsurance(InsuranceEntity entity) {
		if (entity.getTotalAmount() == null) {
			entity.setTotalAmount(BigDecimal.ZERO);
		}
		if (entity.getTotalPremium() == null) {
			entity.setTotalPremium(BigDecimal.ZERO);
		}
		if (entity.getAlreadyPremium() == null) {
			entity.setAlreadyPremium(BigDecimal.ZERO);
		}
		entity.setInsuranceState(InsuranceStateEnum.WAIT_ACCEPT);
		PersistUtil.insert(entity);
		return entity.getInsuranceId();
	}

	@Override
	public void updateFinanceInsurance(InsuranceEntity entity) {
		InsuranceEntity persist = inquireInsuranceEntity(entity.getInsuranceId());
		BeanUtils.copyProperties(entity, persist, "totalAmount", "totalPremium");
		PersistUtil.update(persist);
	}

	@Override
	public void renovateFinanceInsurance(InsuranceEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public InsuranceEntity inquireInsuranceEntity(Long insuranceId) {
		return PersistUtil.get(InsuranceEntity.class, insuranceId);
	}

	@Override
	public PageList<InsuranceEntity> inquireInsuranceList(List<Long> insuranceCoverageIdList,String insuranceNumber, Long financeId, String financeNumber, Integer beginDate, Integer endDate, String applicant,String coverageName,String insuranceCompany, InsuranceStateEnum insuranceState, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(InsuranceEntity.class);
		
		if (!insuranceCoverageIdList.isEmpty()) {
			criteria.add(Restrictions.in("insuranceId",insuranceCoverageIdList ));
		}
			
		if (StringUtils.isNotBlank(insuranceNumber)) {
			criteria.add(Restrictions.like("insuranceNumber", "%" + insuranceNumber + "%"));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (StringUtils.isNotBlank(applicant)) {
			criteria.add(Restrictions.like("applicant", "%" + applicant + "%"));
		}
		
		if(StringUtils.isNotBlank(insuranceCompany)){
			criteria.add(Restrictions.like("insuranceCompany", "%" + insuranceCompany + "%"));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("insuranceDate", beginDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("insuranceDate", endDate));
		}
		if (insuranceState != null) {
			criteria.add(Restrictions.eq("insuranceState", insuranceState));
		}
		criteria.addOrder(Order.asc("insuranceDate"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countInsurances() {
		DetachedCriteria criteria = DetachedCriteria.forClass(InsuranceEntity.class);
		criteria.add(Restrictions.eq("insuranceState", InsuranceStateEnum.WAIT_ACCEPT));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("insuranceId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public InsuranceEntity inquireInsuranceEntityByFinanceId(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(InsuranceEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		List<InsuranceEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

}
